﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using CarlosMisfortune.model;
using TileEngine;
using Microsoft.Xna.Framework.Graphics;

namespace CarlosMisfortune.view
{
    public class GameObjectView
    {
        #region Fields
        protected Dictionary<string, AnimationStrip> m_animations = new Dictionary<string, AnimationStrip>();
        protected string m_currentAnimation;
        #endregion

        #region Helper Methods
        private void updateAnimation(GameTime a_gameTime)
        {
            if (m_animations.ContainsKey(m_currentAnimation))
            {
                if (m_animations[m_currentAnimation].FinishedPlaying)
                {
                    PlayAnimation(m_animations[m_currentAnimation].NextAnimation);
                }
                else
                {
                    m_animations[m_currentAnimation].Update(a_gameTime);
                }
            }
        }
        #endregion

        #region Public Methods
        public void PlayAnimation(string a_name)
        {
            if (a_name != null && m_animations.ContainsKey(a_name))
            {
                m_currentAnimation = a_name;
                m_animations[a_name].Play();
            }
        }

        //Move to GameObject Class!!!
        public virtual void Update(GameTime a_gameTime, GameObject a_obj)
        {
            if (!a_obj.Enabled)
                return;

            float elapsedTime = (float)a_gameTime.ElapsedGameTime.TotalSeconds;

            updateAnimation(a_gameTime);
            if (a_obj.Velocity.Y != 0)
            {
                a_obj.OnGround = false;
            }

            Vector2 moveAmount = a_obj.Velocity * elapsedTime;

            moveAmount = Collision.HorizontalCollisionTest(moveAmount, a_obj);
            moveAmount = Collision.VerticalCollisionTest(moveAmount, a_obj);

            Vector2 newPos = a_obj.WorldPos + moveAmount;
            //TODO: FIXA!!
            //newPos = new Vector2(
            //    MathHelper.Clamp(newPos.X, 0,
            //        Camera.WorldRectangle.Width - a_obj.FrameWidth),
            //    MathHelper.Clamp(newPos.Y, 2 * (-TileMap.TileHeight),
            //        Camera.WorldRectangle.Height - a_obj.FrameHeight));

            a_obj.WorldPos = newPos;
        }

        public void Draw(SpriteBatch a_spriteBatch, GameObject a_obj, float a_scale)
        {
            if (!a_obj.Enabled)
                return;

            if (m_animations.ContainsKey(m_currentAnimation))
            {
                SpriteEffects effect = SpriteEffects.None;
                if (a_obj.Flipped)
                {
                    effect = SpriteEffects.FlipHorizontally;
                }

                //a_spriteBatch.Draw(
                //    m_animations[m_currentAnimation].Texture,
                //    Camera.WorldToScreen(a_obj.WorldRectangle),
                //    m_animations[m_currentAnimation].FrameRectangle,
                //    Color.White, 0.0f, Vector2.Zero, effect, a_obj.DrawDepth);

                a_spriteBatch.Draw(
                    m_animations[m_currentAnimation].Texture,
                    new Vector2(Camera.WorldToScreen(a_obj.WorldRectangle).X, Camera.WorldToScreen(a_obj.WorldRectangle).Y),
                    m_animations[m_currentAnimation].CurrentRectangle,
                    Color.White, 0.0f, Vector2.Zero, a_scale, effect, a_obj.DrawDepth);
            }
        }
        #endregion
    }
}
